package com.gang.ssm.bos.system.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gang.ssm.bos.common.Page;
import com.gang.ssm.bos.core.anotation.Permission;
import com.gang.ssm.bos.core.toolbox.ajax.AjaxResult;
import com.gang.ssm.bos.system.bean.Staff;
import com.gang.ssm.bos.system.service.StaffService;

/**
 * 取派员
 * @author Administrator
 *
 */
@RequestMapping("/staff")
@Controller
public class StaffController extends BosController {
	
	@Autowired
	private StaffService staffService;
	
	private Logger logger = Logger.getLogger(StaffController.class);
	
	@RequestMapping("/list")
	public String list(String name, Page<Staff> page, HttpServletRequest request) {
		
		try {
			staffService.pageQuery(page, name);
			request.setAttribute("request", page);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
		}
		return "system/base/staffList";
	}
	
	@Permission({ADMIN})
	@ResponseBody
	@RequestMapping("/updateDelTag")
	public AjaxResult updateDelTag(Staff staff) {
		try {
			//因为haspda有默认初始值,因此要设为null,避免它也被修改
			staff.setHaspda(null);
			staffService.updateDelTagByStaffId(staff);
			return success("操作成功!");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
		}
		return fail("操作失败!");
	}
	
	@RequestMapping("/staff")
	public String staff(Integer id, Map<String, Object> map) {
		if(id != null) {
			map.put("staff", staffService.findByStaffId(id));
		}
		return "system/base/staff";
	}
	
	@Permission({ADMIN})
	@ResponseBody
	@RequestMapping("/save")
	public AjaxResult save(Staff staff) {
		try {
			staffService.saveOrUpdate(staff);
			return success("保存成功!");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
		}
		
		return fail("保存失败!");
	}
	
	@ResponseBody
	@RequestMapping("/listAjax")
	public AjaxResult findAll() {
		try {
			List<Staff> staffs = staffService.findAll();
			return success(staffs);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
		}
		
		return fail("读取取派员信息出错!");
	}

}
